//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef _TIMERINT_H_
#define _TIMERINT_H_

#include <ddk.h>
#include "arm_interrupt.h"

typedef	unsigned int    UNS_32;
typedef	signed int      INT_32;

// Timer Module Register Structure
typedef struct
{
    volatile UNS_32     load;       /* RW */
    volatile UNS_32     value;      /* RO */
    volatile UNS_32     control;    /* RW */
    volatile UNS_32     clear;      /* WO */
} TIMERREGS;

// Interrupt Controller Module Register Structure
typedef struct
{
	volatile UNS_32		status;
	volatile UNS_32		rawstatus;
	volatile UNS_32		enableset;
	volatile UNS_32		enableclear;
} INTCREGS;

#define _BIT(n)	      (((UNS_32)(1)) << (n))

// Timer Control Register Bit Fields
#define TIMER_CTRL_ENABLE       _BIT(7)
#define TIMER_CTRL_DISABLE      (0)
#define TIMER_CTRL_PERIODIC     _BIT(6)
#define TIMER_CTRL_FREERUN      (0)


// timer mod
typedef enum {
    FREE_RUNNING = TIMER_CTRL_FREERUN,
    PERIODIC     = TIMER_CTRL_PERIODIC,
} TIMER_MODE;

#define APB_BASE      (0x80000000)
#define TIMER1_BASE   (APB_BASE + 0x0C00)
#define TIMER2_BASE   (APB_BASE + 0x0C20)
#define TIMER3_BASE   (APB_BASE + 0x0C80)
#define INTC_BASE     (APB_BASE + 0x0500)
#define TIMER1        ((TIMERREGS *)(TIMER1_BASE))
#define TIMER2        ((TIMERREGS *)(TIMER2_BASE))
#define TIMER3        ((TIMERREGS *)(TIMER3_BASE))

// Interrupt Controller
#define INTC          ((INTCREGS *)(INTC_BASE))

#define INTC_TC3OINTR_BIT       22

#define LH7A400_BASE_CLOCK      (14745600)

// derived clocks
#define CLKSC_XTAL_IN           (LH7A400_BASE_CLOCK)
#define CLKSC_TIMER3_CLK        (CLKSC_XTAL_IN / 2)

const INT_32 TSTTIMES = 2000;

EXTERN_C INT_32 TstIndex;
EXTERN_C INT_32 EndValue[TSTTIMES];

EXTERN_C INT_32 g_nMaxLatency;
EXTERN_C INT_32 g_nTemp;

EXTERN_C INT_32 TstIndexBak;
EXTERN_C INT_32 g_nMaxLatencyBak;

EXTERN_C DzEvent g_event;
EXTERN_C DzEvent g_eventIST;

EXTERN_C UNS_32 g_uFlags;


void ReadTimer3();

void TimerIsr(irq_t irq, void *pvDevice, InterruptContext *pContext);

Int32 ThreadFunc(void* arg);
Int32 IST(void* arg);

#endif // _TIMERINT_H_
